package com.student.system.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.CommonDataSource;
import java.sql.Connection;
import java.sql.SQLException;

//工具类
public class C3P0Untils {
    private static ComboPooledDataSource dataSource=new ComboPooledDataSource();
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    public static ComboPooledDataSource getDataSource(){
        return dataSource   ;
    }

    public static Connection getConnection(){
        Connection connection = threadLocal.get();
        if(connection==null){
            try {
                connection=getDataSource().getConnection();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            threadLocal.set(connection);
        }
        return connection;
    }
}
